package com.tmt.paymentcenter.web.configure;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tmt.fileservice.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.ui.context.support.ResourceBundleThemeSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.theme.CookieThemeResolver;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

/**
 * 常规 web 配置，包括主题，国际化等等。
 * Created by Stark on 2017/3/1.
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

	@Autowired
	private MessageSource messageSource;

	@Autowired
	private IFileService fileService;

	/**
	 * 本地解析器
	 */
	@Bean
	public LocaleResolver localeResolver() {
		CookieLocaleResolver localeResolver = new CookieLocaleResolver();
		localeResolver.setDefaultLocale(Locale.CHINESE);
		localeResolver.setCookieName("locale"); // 指定 cookie 中的名称
		return localeResolver;
	}

	/**
	 * 本地变更拦截器
	 */
	@Bean
	public LocaleChangeInterceptor localeChangeInterceptor() {
		LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
		lci.setParamName("locale");
		return lci;
	}

	/**
	 * 主题资源
	 */
	@Bean
	public ResourceBundleThemeSource themeSource() {
		ResourceBundleThemeSource themeSource = new ResourceBundleThemeSource();
		themeSource.setDefaultEncoding("UTF-8");
		themeSource.setBasenamePrefix("config/theme-");
		return themeSource;
	}

	/**
	 * 主题解析器
	 */
	@Bean
	public CookieThemeResolver themeResolver() {
		CookieThemeResolver resolver = new CookieThemeResolver();
		resolver.setDefaultThemeName("default");
		resolver.setCookieName("theme-cookie");
		return resolver;
	}

	/**
	 * 主题变更拦截器
	 */
	@Bean
	public ThemeChangeInterceptor themeChangeInterceptor() {
		ThemeChangeInterceptor interceptor = new ThemeChangeInterceptor();
		interceptor.setParamName("theme");
		return interceptor;
	}

	@Bean
	public LocalValidatorFactoryBean mvcValidator(MessageSource messageSource) {
		LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
		factory.setValidationMessageSource(messageSource);
		return factory;
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(themeChangeInterceptor());
		registry.addInterceptor(localeChangeInterceptor());
	}


	@Override
	public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
		ObjectMapper objectMapper = new ObjectMapper();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		objectMapper.setDateFormat(df);

		MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper);
		converter.setSupportedMediaTypes(Arrays.asList(
				new MediaType[]{new MediaType("application", "json", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET),
						new MediaType("text", "html", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET)}));
		messageConverters.add(converter);
	}

}
